define(function(require, exports, module) {
	var middlware = require('bigfoot/middlware');
	middlware.provide('common', $);
	
	function $(){}

	$.get = require('bigfoot/dhtml/request').get;
	$.post = require('bigfoot/dhtml/request').post;

	$.msg = require('bigfoot/widget/dialog').ok;
	$.alert = require('bigfoot/widget/dialog').alert;
	$.error = require('bigfoot/widget/dialog').error;

	// for IE8
	$.eachChild = function(el, fn){
		for(var node, childNodes = el.childNodes, i = 0, j = childNodes.length; i < j; i++){
			// for ie8-
			node = childNodes[i];

			if(node.nodeType == 1){
				var _forResult = fn(node, i, j, childNodes);
				
				if(_forResult === true)continue; // 使得 true = continue 来控制 for 循环	
				if(_forResult === false)break; // 使得 false = break 来控制 for 循环	
			}
		}
	}
	
	var tpl = require('bigfoot/dhtml/tpl');
	$.databinding_List = function(_tpl, data, el){
        var renderList = Object.create(tpl.renderList);
        renderList.tpl = _tpl;
        renderList.data = data;
        renderList.apply(el);
	}
	$.replace = function(_tpl, data, el){
		el.innerHTML = tpl.replace(_tpl, data);
	}

	$.databind = function(url, p, _tpl, el, result_key, orderFn, callback, preCallack){
		el.innerHTML = '';
		$.get(url, function(json){
			var data = json.root || json[result_key];
			
			preCallack && preCallack(data);
			
			if(data.length){
				
				orderFn && typeof orderFn == 'function' && data.sort(orderFn);
				orderFn && typeof orderFn == 'boolean' && data.sort();

				$.databinding_List(_tpl, data, el);
			}else console.log(url + '没数据');

			callback && callback(data);

		}, p);
	}

	$.pressedStatable = function(el){
		$.eachChild(el.parentNode, function(_el){
			if(_el == el)_el.addCls('selected');else _el.removeCls('selected');
		});
	}


	$.height = function(el){
		return el.getBoundingClientRect().height;
	}
	module.exports = $;

	$.hook = function(cssSelector, isAll){
		if(isAll){
			var els = parent.document.querySelectorAll(cssSelector); 
			return els.length ? els : document.querySelectorAll(cssSelector);
		}else{
			return parent.document.querySelector(cssSelector) || document.querySelector(cssSelector);
		}
	}


	// 平板的怎么样处理？如果屏幕尺寸很大，是否需拉伸图片

	$.getImgWidthByPercentage = function (percentage){
		var imgWidth = window.innerWidth / percentage;
		
		if(window.devicePixelRatio)
			imgWidth *= window.devicePixelRatio;
		
		return parseInt(imgWidth);
	}
});